package com.angus.modules.front.repository;

import com.angus.api.front.domain.FrontUser;
import com.angus.common.core.utils.DateUtils;
import com.angus.modules.front.mapper.FrontUserMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author liyanan
 * @date 2024/10/19 4:32 PM
 * @description
 */
@Repository
public class FrontUserRepository extends ServiceImpl<FrontUserMapper, FrontUser> {

    public FrontUser selectUserByUserName(String username) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(FrontUser.class)
                .eq(FrontUser::getUserName, username)
                .last("limit 1"));
    }


    public boolean checkUserNameUnique(FrontUser frontUser) {

        FrontUser user = baseMapper.selectOne(Wrappers.lambdaQuery(FrontUser.class)
                .eq(FrontUser::getUserName, frontUser.getUserName())
                .last("limit 1"));
        return !Objects.nonNull(user);
    }


    public boolean registerUser(FrontUser frontUser) {
        return baseMapper.insert(frontUser) > 0;
    }

    public boolean updateUserProfile(FrontUser frontUser) {

        LambdaUpdateWrapper<FrontUser> updateWrapper = Wrappers.lambdaUpdate(FrontUser.class);

        if (Objects.nonNull(frontUser.getNickName())) {
            updateWrapper.set(FrontUser::getNickName, frontUser.getNickName());
        }
        if (Objects.nonNull(frontUser.getRecommendCode())) {
            updateWrapper.set(FrontUser::getRecommendCode, frontUser.getRecommendCode());
        }
        if (Objects.nonNull(frontUser.getPhoneNumber())) {
            updateWrapper.set(FrontUser::getPhoneNumber, frontUser.getPhoneNumber());
        }
        if (Objects.nonNull(frontUser.getUserEmail())) {
            updateWrapper.set(FrontUser::getUserEmail, frontUser.getUserEmail());
        }
        if(Objects.nonNull(frontUser.getUserType())){
            updateWrapper.set(FrontUser::getUserType, frontUser.getUserType());
        }
        if(StringUtils.isNotBlank(frontUser.getAvatar())){
            updateWrapper.set(FrontUser::getAvatar, frontUser.getAvatar());
        }
        if(StringUtils.isNotBlank(frontUser.getPassword())){
            updateWrapper.set(FrontUser::getPassword, frontUser.getPassword());
        }
        if(StringUtils.isNotBlank(frontUser.getLoginIp())){
            updateWrapper.set(FrontUser::getLoginIp, frontUser.getLoginIp());
        }
        if (Objects.nonNull(frontUser.getStatus())){
            updateWrapper.set(FrontUser::getStatus, frontUser.getStatus());
        }
        if(Objects.nonNull(frontUser.getLoginDate())){
            updateWrapper.set(FrontUser::getLoginDate, frontUser.getLoginDate());
        }
        if(StringUtils.isNotBlank(frontUser.getUpdateBy())){
            updateWrapper.set(FrontUser::getUpdateBy, frontUser.getUpdateBy());
        }
        if(StringUtils.isNotBlank(frontUser.getRemark())){
            updateWrapper.set(FrontUser::getRemark, frontUser.getRemark());
        }
        updateWrapper.set(FrontUser::getUpdateTime, DateUtils.getNowDate());

        // 更新条件
        updateWrapper.eq(FrontUser::getUserId, frontUser.getUserId());
        return baseMapper.update(null,updateWrapper)>0;
    }

    public FrontUser selectUserByPhoneNum(String device) {

        return baseMapper.selectOne(Wrappers.lambdaQuery(FrontUser.class)
                .eq(FrontUser::getPhoneNumber, device)
                .last("limit 1"));
    }

    public FrontUser selectUserByEmailNum(String device) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(FrontUser.class)
                .eq(FrontUser::getUserEmail, device)
                .last("limit 1"));
    }

    public List<FrontUser> queryAllUsers(String keyword, Integer status) {
        return baseMapper.getUserList(keyword, status);
    }

    public boolean deleteUserByIds(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    public FrontUser queryUserByEmail(String email) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(FrontUser.class)
                .eq(FrontUser::getUserEmail, email)
                .last("limit 1"));
    }

    public List<FrontUser> queryUserStatistics() {

      return   baseMapper.selectList(Wrappers.lambdaQuery(FrontUser.class).eq(FrontUser::getDelFlag, 0));

    }
}
